#include "main.h"

bool gb_TestFlag;

bool gb_TestOFF;
bool gb_tcomd2;
bool gassta;
u8   gu8_tcomd2error;
u8   gu8_TestStateCnt;

u8 gu8_TestRecved[5];
u16 tempr_LAD;
u16 tempr_RAD;
u16 gans1_AD;
u16 gans2_AD;
u16 kbox_AD;
u16 zbox_AD;
u16 zbottom_AD;
/*************************************************/
void update_gans1(void)
{
	static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 ad = 0;
    static u16 temp = 0;
	u16 v;

	temp = get_average_ad(FGS1_CH);

	val[ct & 0x3] = temp;

	if (ct < 3)
	{
		ad = temp;
	}
	else
	{
		v = val[0] + val[1] + val[2] + val[3];

		ad = v >> 2;						//除4

		ct = (ct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
	}
    gans1_AD = ad;
        
}
void update_gans2(void)
{
	static u8 ct = 0;
    static u16 val[4] = {0,0,0,0};
    static u16 ad = 0;
    static u16 temp = 0;
	u16 v;

	temp = get_average_ad(FGS2_CH);

	val[ct & 0x3] = temp;

	if (ct < 3)
	{
		ad = temp;
	}
	else
	{
		v = val[0] + val[1] + val[2] + val[3];

		ad = v >> 2;						//除4

		ct = (ct & 0x0f) + 0x10;                // 永不溢出 永 > 3 
	}
    gans2_AD = ad;
        
}
/*************************************************/
void testUARTInit(void)
{
}
/*************************************************/
/*************************************************/
void TestCloseAllOut(void)
{
    GPIO_WriteBit(PIN_BOAEG_IN1, 0);
    GPIO_WriteBit(PIN_BOAEG_IN2, 0);

    // CYCLE_OUTPIN = 0;
    // COLD_OUTPIN = 0;
    // FIREOFF_L_OUTPIN = 1;
    // FIREOFF_R_OUTPIN = 1;
    T16Nx_MAT0Out0Config(T16N2, T16Nx_Out_Low); //
    T16Nx_MAT1Out0Config(T16N2, T16Nx_Out_Low); //  
    T16Nx_SetCNT0(T16N1, 0);
    T16Nx_SetMAT0(T16N1, 0);	         
}
/*************************************************/
void Testsend(void)
{
    u16 crc16_temp;
    Bit8_ut sendtemp;
    //u8 i;
    // if(system_100ms_f)
    //     panel_comm.tosend_f = 1;
    if(gb_TestFlag && panel_comm.tosend_f)
    {
        panel_comm.tosend_f = 0;
        panel_comm.tx_buf[0] = 0x5A;
		panel_comm.tx_buf[1] = 0;
		panel_comm.tx_buf[2] = 0;
		panel_comm.tx_buf[3] = 0xfc;
        panel_comm.tx_buf[4] = 32;        
        panel_comm.tx_buf[5] = INVISION;//软件版本
        sendtemp.byte = 0;
        sendtemp.bits.bit_0 = gassta;//气敏信号
        sendtemp.bits.bit_1 = !shell_err_f;    //破皮检测
        sendtemp.bits.bit_2 = !knobl.sta;       //左旋钮
        sendtemp.bits.bit_3 = !knobr.sta;       //右旋钮
        sendtemp.bits.bit_4 = !doorbake.sta;    //K门开关
        sendtemp.bits.bit_5 = !waterbox.sta;    //水箱开关
        sendtemp.bits.bit_6 = !door.sta;        //Z门开关
        sendtemp.bits.bit_7 = GPIO_ReadBit(POT_INPIN);//防干烧开关
        panel_comm.tx_buf[6] = sendtemp.byte;
        sendtemp.byte = 0;
        if(gu8_zerodefault == 0)
            sendtemp.bits.bit_0 = 1;
        sendtemp.bits.bit_1 = motor_rx_err;
        panel_comm.tx_buf[7] = sendtemp.byte;

        panel_comm.tx_buf[8] = HIBYTE(tempr_wall_L); 
        panel_comm.tx_buf[9] = LOBYTE(tempr_wall_L);
        panel_comm.tx_buf[10] = HIBYTE(tempr_wall_R);
        panel_comm.tx_buf[11] = LOBYTE(tempr_wall_R);
        panel_comm.tx_buf[12] = HIBYTE(gans1_AD);
        panel_comm.tx_buf[13] = LOBYTE(gans1_AD);
        panel_comm.tx_buf[14] = HIBYTE(gans2_AD);
        panel_comm.tx_buf[15] = LOBYTE(gans2_AD);
        panel_comm.tx_buf[16] =  board.cur_i_10mA;
        panel_comm.tx_buf[17] = HIBYTE(fire_L_thcp_ad);  
        panel_comm.tx_buf[18] = LOBYTE(fire_L_thcp_ad);     
        panel_comm.tx_buf[19] = HIBYTE(fire_R_thcp_ad);  
        panel_comm.tx_buf[20] = LOBYTE(fire_R_thcp_ad); 
               
        panel_comm.tx_buf[21] = HIBYTE(tempr_top.cur);  
        panel_comm.tx_buf[22] = LOBYTE(tempr_top.cur);
        
        panel_comm.tx_buf[23] = HIBYTE(tempr_bom.cur);  
        panel_comm.tx_buf[24] = LOBYTE(tempr_bom.cur);
        panel_comm.tx_buf[25] = HIBYTE(tempr_box.cur);  
        panel_comm.tx_buf[26] = LOBYTE(tempr_box.cur);       
        panel_comm.tx_buf[27] = HIBYTE(gu16_tubecurt);  
        panel_comm.tx_buf[28] = LOBYTE(gu16_tubecurt);
        panel_comm.tx_buf[29] = 6;  //产品类型
        crc16_temp = CRC16_MODBUS(panel_comm.tx_buf, 30);
        panel_comm.tx_buf[30] = LOBYTE(crc16_temp);
        panel_comm.tx_buf[31] = HIBYTE(crc16_temp);
        panel_comm.sending_f = true;
        panel_comm.tx_ct = 0;
        UART_SendByte(UART1, panel_comm.tx_buf[0]);   
        return;
    }    
}
/*************************************************/
/*************************************************/
void TestProcess(void)
{
    Bit8_ut temp;
    if(gb_TestFlag)
    {
        if(system_100ms_f)
        {
            temp.byte = gu8_TestRecved[0];
            EN15TOGND_OUTPIN = temp.bits.bit_0;
            GPIO_WriteBit(EN15_OUTPIN, temp.bits.bit_1);
            GPIO_WriteBit(KPRE12V_OUTPIN, temp.bits.bit_2);
            GPIO_WriteBit(ADDWZ_OUTPIN,temp.bits.bit_3);
            WASH_OUTPIN = temp.bits.bit_4;
            KEEPWARM_OUTPIN = temp.bits.bit_5;
            COLD_OUTPIN = temp.bits.bit_6;            
            CYCLE_OUTPIN = temp.bits.bit_7;
			
            temp.byte = gu8_TestRecved[1];
            PIN_HT_BOMI = temp.bits.bit_0;
            PIN_HT_BOMO = temp.bits.bit_1;
            PIN_HT_LEFT = temp.bits.bit_2;
            PIN_HT_OTOP = temp.bits.bit_3;
            PIN_HT_TOP = temp.bits.bit_4;
            PIN_HT_PRE = temp.bits.bit_5;
            GPIO_WriteBit(FWDCOMM_OUTPIN,temp.bits.bit_7);

            temp.byte = gu8_TestRecved[2];    
            GPIO_WriteBit(PIN_BOAEG_IN1, temp.bits.bit_0);
            GPIO_WriteBit(PIN_BOAEG_IN2, temp.bits.bit_1);
            GPIO_WriteBit(FIREOFF_R_OUTPIN, !temp.bits.bit_2);
            GPIO_WriteBit(FIREOFF_L_OUTPIN, !temp.bits.bit_3);            
            GPIO_WriteBit(PREATMOS_OUTPIN, temp.bits.bit_4);
            KBOX_LIGHT_OUTPIN = temp.bits.bit_5;
            GPIO_WriteBit(ZBOX_LIGHT_OUTPIN, temp.bits.bit_6);  
            if(temp.bits.bit_7)   
            {
                T16Nx_MAT0Out0Config(T16N2, T16Nx_Out_High); //
                T16Nx_MAT1Out0Config(T16N2, T16Nx_Out_High); //       
                T16Nx_SetMAT0(T16N2, 200);                         
            }     
            else
            {
                T16Nx_MAT0Out0Config(T16N2, T16Nx_Out_Low); //
                T16Nx_MAT1Out0Config(T16N2, T16Nx_Out_Low); //          
                T16Nx_SetCNT0(T16N1, 0);
                T16Nx_SetMAT0(T16N1, 0); 				
            }
            //LIGHT_OUTPIN = temp.bits.bit_7;

            temp.byte = gu8_TestRecved[4];
            GPIO_WriteBit(LOCKZ_OUTPIN, temp.bits.bit_0);                      
            GPIO_WriteBit(LOCKK_OUTPIN, temp.bits.bit_1);            
            GPIO_WriteBit(XNLIGHT_L1_OUTPIN,temp.bits.bit_2);
            GPIO_WriteBit(XNLIGHT_L2_OUTPIN,temp.bits.bit_3);
            GPIO_WriteBit(XNLIGHT_R1_OUTPIN,temp.bits.bit_4);
            GPIO_WriteBit(XNLIGHT_R2_OUTPIN,temp.bits.bit_5);

            gans1_AD = get_average_ad(FGS1_CH);
            //gans2_AD = get_average_ad(FGS2_CH);
            gu16_tubecurt = get_average_ad(TUBE_CURRENT_CH);
            Testsend();              
        }
    }
}
